/**
 * @author xushiyu
 * @createDate 2016年11月11日
 */
package com.microlecture.common;


import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据包
 */
public class DataPacket {
    public static final String RESULT_EKY_DATA = "data";
    public static final String RESULT_KEY_CODE = "code";
    public static final String RESULT_KEY_MESSAGE = "message";

    public static final String APP_RETURN_SUCCESS_CODE = "1";// 请求处理成功
    public static final String APP_RETURN_FAIL_CODE = "0";// 请求处理失败
    /**
     * session失效
     */
    public static final String APP_SESSION_INVALID_CODE = "-1";

    /**
     * 禁止登录
     */
    public static final String APP_FORBID_LOGIN_CODE = "-2";

    /**
     * 用户名已经存在
     */
    public static final String APP_EXIST_USER_CODE = "2";
    public static final String APP_NOT_EXIST_USER_CODE = "3";


    private DataPacket() {

    }

    /**
     * 返回json
     * @return
     */
    public static Map<String, Object> jsonCode(String code) {
        Map<String, Object> result = new HashMap<>();
        result.put(RESULT_KEY_CODE, code);
        return result;
    }

    /**
     * 返回成功json,数据和消息
     * @return
     */
    public static Map<String, Object> jsonResult() {
        Map<String, Object> result = new HashMap<>();
        result.put(RESULT_KEY_CODE, APP_RETURN_SUCCESS_CODE);
        return result;
    }

    /**
     * 返回成功json,数据和消息
     * @param data
     * @param msg
     * @return
     */
    public static Map<String, Object> jsonResult(Object data, String msg) {
        Map<String, Object> result = new HashMap<>();
        result.put(RESULT_KEY_CODE, APP_RETURN_SUCCESS_CODE);
        result.put(RESULT_KEY_MESSAGE, msg);
        result.put(RESULT_EKY_DATA, data);
        return result;
    }

    /**
     * 返回失败json，只带消息
     * @param msg
     * @return
     */
    public static Map<String, Object> errorJsonResult(String msg) {
        Map<String, Object> result = new HashMap<>();
        result.put(RESULT_KEY_CODE, APP_RETURN_FAIL_CODE);
        result.put(RESULT_KEY_MESSAGE, msg);
        return result;
    }

    /**
     * 返回失败json
     * @return
     */
    public static Map<String, Object> errorJsonResult() {
        Map<String, Object> result = new HashMap<>();
        result.put(RESULT_KEY_CODE, APP_RETURN_FAIL_CODE);
        return result;
    }

    /**
     * 返回失败json
     * @return
     */
    public static Map<String, Object> sessionInvalidJsonResult() {
        Map<String, Object> result = new HashMap<>();
        result.put(RESULT_KEY_CODE, APP_SESSION_INVALID_CODE);
        return result;
    }

    /**
     * 返回成功json，只带数据
     * @param data
     * @return
     */
    public static Map<String, Object> jsonResult(Object data) {
        return jsonResult(data, null);
    }

    /**
     * 返回成功json，只带消息
     * @param msg
     * @return
     */
    public static Map<String, Object> jsonResult(String msg) {
        return jsonResult(null, msg);
    }


    /**
     * 为空的数据表格
     * @return
     */
    public static Map<String, Object> dataGridEmptyResult() {
        Map<String, Object> result = new HashMap<>();
        result.put(Constants.TOTAL, 0L);
        result.put(Constants.ROWS, Collections.emptyList());
        return result;
    }

    /**
     * 返回数据表格
     * @param total
     * @param rows
     * @return
     */
    public static Map<String, Object> dataGridResult(long total, List rows) {
        Map<String, Object> result = new HashMap<>();
        result.put(Constants.TOTAL, total);
        result.put(Constants.ROWS, rows);
        return result;
    }

    /**
     * 增删改结果
     */
    public static Map<String, Object> changeDBResult(int dbResult) {
        Map<String, Object> result = new HashMap<>();
        if(dbResult > 0) {
            result.put(RESULT_KEY_CODE, APP_RETURN_SUCCESS_CODE);
        } else {
            result.put(RESULT_KEY_CODE, APP_RETURN_FAIL_CODE);
        }
        return result;
    }

}
